
package com.cx.bank.manager;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

//导入持久层
import com.cx.bank.dao.BankDaoInterface;
import com.cx.bank.factory.UserDaoFactory;
import com.cx.bank.hibernateModel.PageModel;
import com.cx.bank.hibernateModel.Users;
//导入银行系统模型层
import com.cx.bank.model.MoneyBean;
import com.cx.bank.model.UserBean;
//导入银行系统工具包
import com.cx.bank.util.AccountOverDrawnException;
import com.cx.bank.util.ErrorCodeException;
import com.cx.bank.util.HibernateUtils;
import com.cx.bank.util.InvalidDepositException;
import com.cx.bank.util.MD5;

/**
 * 控制层 实现存款、取款、查询、退出功能
 **/
public class ManagerImpl implements ManagerInterface {
	
	String transaction_details = null;
	String transaction_details2 = null;
	String transaction_time = null;

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
	UserBean user = new UserBean();
	MoneyBean moneyBean =MoneyBean.getInstance();
	
	private static ManagerImpl managerimpl = new ManagerImpl();

	public static synchronized ManagerImpl getInstance() {
		return managerimpl;
	}

	UserDaoFactory DaoFactory;
	private BankDaoInterface mon;

	/* 实现工厂模式 */
	private ManagerImpl() {
		try {
			DaoFactory = UserDaoFactory.getInstance();
			mon = DaoFactory.createFactoryDao();
		} catch (ClassNotFoundException | InstantiationException | IOException e) {
			e.printStackTrace();
		}
	}

	@Override // 查询方法
	public double inquiry(MoneyBean money) {
		return money.getCount();
	}

	// @Override 取款方法
	public void withdrawals(MoneyBean money, String sgetmoney, UserBean user)
			throws AccountOverDrawnException, InvalidDepositException// 异常声明
	{
		Date now = new Date();
		transaction_time = sdf.format(now);
		try {
			// 将从表单中得到的String类型的money强制转换成double类型
			Double getmoney = Double.valueOf(sgetmoney);

			if (getmoney > money.getCount()) {
				throw new ErrorCodeException("withdraw.error.AccountOverDrawnException");// 抛出异常
			} else if (getmoney <= 0) {
				throw new ErrorCodeException("withdraw.error.InvalidDepositException");// 抛出异常
			} else {
				// 将取款金额放入moneyBean中
				money.setCount(money.getCount() - getmoney);

				// 记录交易细节，为后续存入交易记录表中提供数据
				transaction_details = "-" + getmoney + "取款";
				try {
					// 调用持久层的保存当前用户信息的方法，将用户取款后的余额存入数据库中
					mon.savaTransactionDetials2(user.getName(), transaction_details, transaction_time);
					mon.savaInformation(user, money);
				} catch (IOException e) {
					System.out.println("信息存储失败");
				}
			}

		} catch (NumberFormatException e) {
			throw new ErrorCodeException("withdraw.error.NumberFormatException");// 抛出异常
		}
	}

	// 存款方法
	public void deposit(MoneyBean money, Double storemoney, UserBean user) throws InvalidDepositException {
		Date now = new Date();
		transaction_time = sdf.format(now);

		if (storemoney <= 0) {
			throw new InvalidDepositException("请输入大于零的金额!");// 抛出异常
		} else {
			// 将存款金额放入moneyBean中
			money.setCount(money.getCount() + storemoney);

			// 记录交易细节，为后续存入交易记录表中提供数据
			transaction_details = "+" + storemoney + "存款";
			try {
				// 调用持久层的保存当前用户信息的方法，将用户存款后的余额存入数据库中
				mon.savaTransactionDetials2(user.getName(), transaction_details, transaction_time);
				mon.savaInformation(user, money);
			} catch (IOException e) {
				System.out.println("信息存储失败");
			}
		}

	}

	// 转账功能
	public Boolean transfer(MoneyBean money, String sendmoney, String transfername, UserBean user)
			throws AccountOverDrawnException, InvalidDepositException, IOException {
		boolean flag = false;
		Date now = new Date();
		transaction_time = sdf.format(now);
		try {
			// 将从表单中得到的String类型的transfermoney强制转换成double类型
			Double transfermoney = Double.valueOf(sendmoney);
			if (transfermoney > money.getCount()) {
				throw new ErrorCodeException("transfer.error.AccountOverDrawnException");// 抛出异常
			} else if (transfermoney <= 0) {
				throw new ErrorCodeException("transfer.error.InvalidDepositException");// 抛出异常
			} else {
				// 转入账号数据库信息配置保存
				if (!mon.savaInformation2(transfername, transfermoney)) {
					flag = false;
					return flag;
				}
				// 在余额中减去转账金额
				money.setCount(money.getCount() - transfermoney);
				mon.savaInformation(user, money); // 本账号数据库信息配置保存

				transaction_details = "-" + transfermoney + "转账给" + transfername;
				transaction_details2 = "+" + transfermoney + "收到" + user.getName() + "的转账";
				mon.savaTransactionDetials(user.getName(), transfername, transaction_details, transaction_details2,transaction_time);
				flag = true;
			}
		} catch (NumberFormatException e) {
			System.out.println("转账失败，用户输入了非法字符!");
			throw new ErrorCodeException("transfer.error.NumberFormatException");// 抛出异常
		}
		return flag;
	}

	@Override // 登陆方法
	public Boolean login(UserBean user) throws IOException {
		return mon.login(user);
	}

	@Override // 注册方法
	public Boolean register(UserBean user) throws IOException {
		return mon.register(user);
	}

	// 从业务层得到交易明细
	public PageModel findAllTransaction(int pageNo, int pageSize, String name,String orderby){
		return mon.findAllTransaction(pageNo,pageSize,name, orderby);
	}
}